
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements.Experimental;

public class View
{
    public string resPath;
    public GameObject gameObject;

    public Transform transform;
    public Canvas canvas;
    public Action<bool> Close;

    public bool isAddUpdateListenser = false;
    public bool stopAi_open = false; //是否打开
    public bool _enable = false;

    public CanvasScaler cs;

    public T GetComponent<T>(string path) where T : Component
    {
        return this.transform.Find(path).GetComponent<T>();
    }

    public void Init(string resPath,bool isAddUpdateListen = false,Action<bool>close =null,bool stopAI_open = false)
    {
        this.isAddUpdateListenser = isAddUpdateListen;
        this.Close = close;
        this.resPath = resPath;
        this.stopAi_open = stopAI_open;
    }

    
    /// <summary>
    /// 获得某层级下的组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="path"> 层级</param>
    /// <returns></returns>
    public T GeComponent<T>(string path) where T : Component
    {
        return this.transform.GetComponent<T>();

    }
    public virtual void Awake()
    {

    }

    public virtual void OnEnable()
    {
        if(isAddUpdateListenser)
        {
            ViewManager.Instance.OnUpdate += Update;
        }
    }

    public virtual void Start()
    {

    }

    public virtual void Update()
    {

    }

    public virtual void OnDestory()
    {

    }


    public virtual void OnDisable()
    {

    }

    public void SetSize()
    {
        bool b = (Screen.width / (float)Screen.height) > cs.referenceResolution.x /cs.referenceResolution.y;
        if(b)
        {
            cs.matchWidthOrHeight = 1;  //大于说明宽度大了 要按照长度来适配 0 宽 1 长
        }
        else
        {
            cs.matchWidthOrHeight = 0;
        }
    }


    /// <summary>
    /// 设置 image的图片 ->多语言
    /// </summary>
    /// <param name="image"></param>
    /// <param name="sprite"></param>
    public void SetSprite(Image image,string sprite)
    {
        var s = ResourceManager.Instance.Load<Sprite>(sprite);
        image.sprite = s;   


    }

    /// <summary>
    /// 后续也是对接多语言
    /// </summary>
    /// <param name="text"></param>
    /// <param name="content"></param>
    public void SetText(Text text,string content)
    {
        text.text = content;
    }

    /// <summary>
    /// 通过多语言表查找
    /// </summary>
    /// <param name="text"></param>
    /// <param name="content"></param>
    public void SetText(Text text, int id)
    {
        //text.text = content;
    }

}
